import 'dart:io';

import '../fluent.dart';
import '../utils.dart';

String getOperatingSystemName() {
  switch (Platform.operatingSystem) {
    case 'ohos':
      try {
        print('Attempting to get OHOS system name...');
        // 尝试多种可能的getprop属性 - 扩展列表尝试更多可能的OHOS属性
        final List<String> nameProps = [
          'ro.product.system.brand',
          'ro.product.brand',
          'ro.product.manufacturer',
          'ro.product.name',
          'ro.system.build.product',
          'ro.ohos.product.model', // 可能的OHOS特有属性
          'ro.ohos.build.version.ohos',
          'ro.ohos.build.version.name',
          'ro.ohos.build.version.incremental'
        ];
        for (final prop in nameProps) {
          final fluentObj = fluent(null);
          fluentObj.exec('getprop', [prop]);
          final osName = fluentObj.stringValue.trim();
          print('getprop $prop result: $osName');
          if (osName.isNotEmpty && osName.toLowerCase() != 'null') {
            print('--Found valid OS name from $prop: $osName');
            return osName;
          }
        }
        // 尝试更简单的命令，如uname
        try {
          final fluentObj = fluent(null);
          fluentObj.exec('uname', ['-s']);
          final unameResult = fluentObj.stringValue.trim();
          print('uname -s result: $unameResult');
          if (unameResult.isNotEmpty && unameResult.toLowerCase() != 'null') {
            print('--Found valid OS name from uname: $unameResult');
            return unameResult;
          }
        } catch (e) {
          print('uname command failed: $e');
        }
        // 如果getprop命令失败，尝试使用lsb_release命令作为后备方案
        try {
          final fluentObj = fluent(null);
          fluentObj.exec('lsb_release', ['-a']);
          final fluentData = fluent(fluentObj.value)
                ..trim()
                ..stringToMap(':');
          final data = fluentData.mapValue;
          print('lsb_release data: $data');
          final distributorId = fluent(data['Distributor ID']).stringValue;
          print('Distributor ID: $distributorId');
          if (distributorId.isNotEmpty && distributorId.toLowerCase() != 'null') {
            print('--Found valid OS name from lsb_release: $distributorId');
            return distributorId;
          }
        } catch (e) {
          print('lsb_release failed: $e');
        }
        print('OHOS getOperatingSystemName: All methods failed, returning default');
      } catch (e) {
        print('OHOS getOperatingSystemName error: $e');
      }
      // 如果所有方法都失败，返回默认的'OHOS'
      print('Operating system name: OHOS (default)');
      return 'OHOS';
    case 'android':
    case 'linux':
      final data = (fluent(exec('lsb_release', ['-a']))
            ..trim()
            ..stringToMap(':'))
          .mapValue;
      return fluent(data['Distributor ID']).stringValue;
    case 'macos':
      final data = (fluent(exec('sw_vers', []))
            ..trim()
            ..stringToMap(':'))
          .mapValue;
      return fluent(data['ProductName']).stringValue;
    case 'windows':
      final data = wmicGetValueAsMap('OS', ['Caption'])!;
      return fluent(data['Caption']).stringValue;
    default:
      notSupportedError();
  }
}

String getOperatingSystemVersion() {
  switch (Platform.operatingSystem) {
    case 'ohos':
      try {
        // 尝试多种可能的getprop版本属性 - 扩展列表尝试更多可能的OHOS属性
        final List<String> versionProps = [
          'ro.build.version.release',
          'ro.system.build.version',
          'ro.build.version.incremental',
          'ro.product.build.version',
          'ro.build.version.sdk_int',
          'ro.build.display.id',
          'ro.ohos.build.version.id', // 可能的OHOS特有版本属性
          'ro.ohos.build.version.ohos',
          'ro.ohos.build.version.api_level',
          'ro.ohos.build.version.release_type',
          'ro.ohos.build.date',
          'ro.ohos.build.date.utc'
        ];
        
        for (final prop in versionProps) {
          final fluentObj = fluent(null);
          fluentObj.exec('getprop', [prop]);
          final version = fluentObj.stringValue.trim();
          if (version.isNotEmpty && version.toLowerCase() != 'null') {
            return version;
          }
        }
        
        // 尝试更简单的命令，如uname -r获取内核版本
        try {
          final fluentObj = fluent(null);
          fluentObj.exec('uname', ['-r']);
          final kernelVersion = fluentObj.stringValue.trim();
          print('uname -r result: $kernelVersion');
          if (kernelVersion.isNotEmpty && kernelVersion.toLowerCase() != 'null') {
            print('Found valid kernel version: $kernelVersion');
            return 'Kernel: $kernelVersion';
          }
        } catch (e) {
          print('uname -r command failed: $e');
        }
        
        // 如果getprop命令失败，尝试使用lsb_release命令作为后备方案
        try {
          final fluentObj = fluent(null);
          fluentObj.exec('lsb_release', ['-a']);
          final fluentData = fluent(fluentObj.value)
                ..trim()
                ..stringToMap(':');
          final data = fluentData.mapValue;
          print('lsb_release data for version: $data');
          final release = fluent(data['Release']).stringValue;
          if (release.isNotEmpty && release.toLowerCase() != 'null') {
            print('Found valid OS version from lsb_release: $release');
            return release;
          }
        } catch (e) {
          print('lsb_release failed: $e');
        }
      } catch (e) {
        print('OHOS getOperatingSystemVersion error: $e');
      }
      // 如果所有方法都失败，返回有意义的默认版本标识
      print('Operating system version: OHOS Version Unknown (default)');
      return 'OHOS Version Unknown';
    case 'android':
    case 'linux':
      final data = (fluent(exec('lsb_release', ['-a']))
            ..trim()
            ..stringToMap(':'))
          .mapValue;
      return fluent(data['Release']).stringValue;
    case 'macos':
      final data = (fluent(exec('sw_vers', []))
            ..trim()
            ..stringToMap(':'))
          .mapValue;
      return fluent(data['ProductVersion']).stringValue;
    case 'windows':
      final data = wmicGetValueAsMap('OS', ['Version'])!;
      return fluent(data['Version']).stringValue;
    default:
      notSupportedError();
  }
}
